package com.power.handler;

import com.power.result.R;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authorization.AuthorizationDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author 海晨忆
 * @Des 全局异常捕获
 * @Date 2025/8/13 14:25
 */
@RestControllerAdvice //aop拦截标注了@RestController的Controller中所有的方法
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public R handException(Exception e) {
        e.printStackTrace();
        return R.FAIL("服务器异常，错误原因：" + e.getMessage());
    }

    /**
     * 异常的精确匹配，先精确匹配，匹配不到了，就找弗雷的异常处理
     *
     * @return
     */
    @ExceptionHandler(value = DataAccessException.class)
    public R handException2(DataAccessException e) {
        e.printStackTrace();
        return R.FAIL(e.getMessage());
    }

    @ExceptionHandler(value = AuthorizationDeniedException.class)
    public R handException2(AuthorizationDeniedException e) {
        e.printStackTrace();
        return R.FAIL("权限不足");
    }

}
